/**
 * 
 */
package xl.work.dev.network;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import xl.work.dev.file.Read;
import xl.work.dev.file.Write;
import xl.work.dev.time.DateUtil;
import xl.work.dev.time.IdGen;

/**
 * <p>
 * Description:
 * </p>
 *
 * @author zhoufan
 * @date 2018-08-10
 */
public class SSProxy {
    private final IdGen idGen = IdGen.get();
    
    @Test
    public void getJson() {
        List<String> list = Read.file2ListString("data/proxy/proxy.txt");
        JSONObject configObject = JSONObject.parseObject(Read.file2String("data/server-gen.json"));
        System.out.println(configObject.get("configs"));
        List<Map<String, Object>> servers = new ArrayList<>();
        
        /*
         * "remarks" : "",
            "id" : "CF04EBD1521C0EB499D7BD00A1694328",
            "server" : "zzzz sgp.bandeng.org",
            "server_port" : 29383,
            "server_udp_port" : 0,
            "password" : "38MISG",
            "method" : "chacha20",
            "protocol" : "origin",
            "protocolparam" : "",
            "obfs" : "plain",
            "obfsparam" : "",
            "remarks_base64" : "",
            "group" : "FreeSSR-public",
            "enable" : true,
            "udp_over_tcp" : false
         */
        for (String string : list) {
            String[] strs = string.split("\\s+");
            //System.out.println(Arrays.toString(strs));
            Map<String, Object> map = new HashMap<>();
            map.put("server", strs[1]);
            map.put("server_port", strs[2]);
            map.put("password", strs[3]);
            map.put("method", strs[4]);
            
            map.put("remarks" , "");
            map.put("id" , idGen.nextId());
            map.put("server_udp_port" , 0);
            map.put("protocol" , "origin");
            map.put("protocolparam" , "");
            map.put("obfs" , "plain");
            map.put("obfsparam" , "");
            map.put("remarks_base64" , "");
            map.put("group" , "FreeSS-"+DateUtil.getStringDateShort());
            map.put("enable" , true);
            map.put("udp_over_tcp" , false);            //System.out.println(map);
            servers.add(map);
        }
        //System.out.println(JSON.toJSONString(servers, true));
        configObject.put("configs", servers);
        Write.createByWriter("data/server-gen-"+DateUtil.getStringDateShort()+".json",
                JSON.toJSONString(configObject, true));
//        String[] strs = list.get(0).split("\\s+");
//        for (String s : strs) {
//            System.out.println(s);
//        }
    }

}
